package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.threadlocal.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    /**
     * 素材上传
     *
     * @param file
     * @return
     */
    @Override
    @Transactional
    public ResponseResult upload(MultipartFile file) {
        //1.上传图片到minio中
        //TODO
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String qz = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        try {
            InputStream inputStream = file.getInputStream();
            String url = fileStorageService.uploadImgFile(qz, suffix, inputStream);
            WmUser user = WmThreadLocalUtil.getUser();
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(user.getId());
            wmMaterial.setUrl(url);
            wmMaterial.setType((short) 0);
            wmMaterial.setCreatedTime(new Date());
            wmMaterial.setIsCollection((short) 0);
            this.save(wmMaterial);
            LambdaQueryWrapper<WmMaterial> queryWrapper = Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getUrl, url);
            WmMaterial one = this.getOne(queryWrapper);
            return ResponseResult.okResult(one);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 查询素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
       //TODO
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> queryWrapper = Wrappers.<WmMaterial>lambdaQuery().eq(WmMaterial::getIsCollection, dto.getIsCollection());
        IPage<WmMaterial> page1 = this.page(page, queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page1.getTotal());
        pageResponseResult.setData(page1.getRecords());
        return pageResponseResult;
    }

    /**
     * 删除素材
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(501,"参数失效");
        }
        WmMaterial wmMaterial = this.getById(id);
        if(wmMaterial==null){
            return ResponseResult.errorResult(1002,"数据不存在");
        }
        List<WmNewsMaterial> wmNewsMaterialList = wmNewsMaterialMapper.selectList(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if(wmNewsMaterialList.size()!=0){
            return ResponseResult.errorResult(501,"文件删除失败");
        }
        this.removeById(id);
        return ResponseResult.okResult(null);
    }

    /**
     * 取消收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult cancel(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(501,"参数失效");
        }
        WmMaterial wmMaterial = this.getById(id);
        wmMaterial.setIsCollection((short) 0);
        this.updateById(wmMaterial);
        return ResponseResult.okResult(null);
    }

    /**
     * 收藏
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult collect(Integer id) {
        if(id==null){
            return ResponseResult.errorResult(501,"参数失效");
        }
        WmMaterial wmMaterial = this.getById(id);
        wmMaterial.setIsCollection((short) 1);
        this.updateById(wmMaterial);
        return ResponseResult.okResult(null);
    }
}
